/**
 * //Given a string s containing just the characters '(', ')', '{', '}', '[' and ']
 * //', determine if the input string is valid.
 * //
 * // An input string is valid if:
 * //
 * //
 * // Open brackets must be closed by the same type of brackets.
 * // Open brackets must be closed in the correct order.
 * // Every close bracket has a corresponding open bracket of the same type.
 * //
 * //
 * //
 * // Example 1:
 * //
 * //
 * //Input: s = "()"
 * //Output: true
 * //
 * //
 * // Example 2:
 * //
 * //
 * //Input: s = "()[]{}"
 * //Output: true
 * //
 * //
 * // Example 3:
 * //
 * //
 * //Input: s = "(]"
 * //Output: false
 * //
 * //
 * //
 * // Constraints:
 * //
 * //
 * // 1 <= s.length <= 10⁴
 * // s consists of parentheses only '()[]{}'.
 * //
 * //
 * // Related Topics 栈 字符串 👍 3548 👎 0
 */

package com.xixi.dataStructure.stack;

import java.util.Stack;

public class ID00020ValidParentheses {
    public static void main(String[] args) {
        Solution solution = new ID00020ValidParentheses().new Solution();
    }


    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {

        Stack<Character> bracketLeft = new Stack<>();

        public boolean isValid(String s) {
            boolean res = true;
            char[] sChar = s.toCharArray();
            char leftBrac;
            for (char bracket : sChar) {
                switch (bracket) {
                    case '(':
                    case '{':
                    case '[':
                        bracketLeft.push(bracket);
                        break;
                    case ')':
                        if (bracketLeft.isEmpty()) return false;
                        leftBrac = bracketLeft.pop();
                        if (leftBrac != '(') {
                            return false;
                        }
                        break;
                    case '}':
                        if (bracketLeft.isEmpty()) return false;
                        leftBrac = bracketLeft.pop();
                        if (leftBrac != '{') {
                            return false;
                        }
                        break;
                    case ']':
                        if (bracketLeft.isEmpty()) return false;
                        leftBrac = bracketLeft.pop();
                        if (leftBrac != '[') {
                            return false;
                        }
                        break;
                }
            }

            if (!bracketLeft.isEmpty()) return false;

            return res;


        }
    }
//leetcode submit region end(Prohibit modification and deletion)


}